package com.qinghe.pro.controller.main.api;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qinghe.pro.entity.api.Api;
import com.qinghe.pro.service.api.ApiColumnService;
import com.qinghe.pro.service.api.ApiService;
import com.qinghe.utils.AjaxObject;
import com.qinghe.utils.Page;

@Controller
@RequestMapping("/manager/api")
public class ApiController {

	@Autowired
	@Qualifier("apiServiceImpl")
	private ApiService apiService;

	@Autowired
	@Qualifier("apiColumnServiceImpl")
	private ApiColumnService apiColumnService;

	private static final String CREATE = "manager/api/create";
	private static final String UPDATE = "manager/api/update";
	private static final String VIEW = "manager/api/view";
	private static final String LIST = "manager/api/list";

	@RequiresPermissions("Api:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate(Map<String, Object> map) {
        map.put("columnList", apiColumnService.findMainAsTree());
		return CREATE;
	}

	@RequiresPermissions("Api:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody AjaxObject create(Api api) {
		apiService.insert(api);

		return AjaxObject.newOk("添加接口成功！");
	}

	@RequiresPermissions("Api:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable Integer id, Map<String, Object> map) {
		Api api = apiService.get(id);

		map.put("api", api);
        map.put("columnList", apiColumnService.findMainAsTree());
		return UPDATE;
	}

	@RequiresPermissions("Api:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody AjaxObject update(Api api) {
		apiService.update(api);

		return AjaxObject.newOk("修改接口成功！");
	}

	@RequiresPermissions("Api:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody AjaxObject delete(@PathVariable Integer id) {
		apiService.delete(id);

		return AjaxObject.newOk("删除接口成功！");
	}

	@RequiresPermissions("Api:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody AjaxObject deleteMany(@RequestParam("ids[]") List<Integer> ids) {
		for (Integer id : ids) {
			apiService.delete(id);
		}

		return AjaxObject.newOk("删除接口成功！");
	}

	@RequiresPermissions("Api:view")
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable Integer id, Map<String, Object> map) {
		Api api = apiService.get(id);
		
		map.put("api", api);
		return VIEW;
	}

	@RequiresPermissions("Api:show")
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Api api, Page page, Map<String, Object> map) {
		List<Api> apiList = apiService.findByPage(api, page);

		map.put("page", page);
		map.put("apiList", apiList);
        map.put("columnList", apiColumnService.findMainAsTree());

		return LIST;
	}

}
